package com.doyens.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doyens.common.CustomException;
import com.doyens.entity.Category;
import com.doyens.entity.Dish;
import com.doyens.entity.Setmeal;
import com.doyens.mapper.CategoryMapper;
import com.doyens.service.CategoryService;
import com.doyens.service.DishService;
import com.doyens.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    //删除业务逻辑

    // 查询  套餐分类下是否有套餐    根据categoryId 查询 Setmeal表
    @Autowired
    private SetmealService   setmealService;

    @Autowired
    private DishService dishService;


    //完善删除分类功能
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish>  dishLambdaQueryWrapper = new LambdaQueryWrapper<>();

        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int countDish = dishService.count(dishLambdaQueryWrapper);
        // 查询  菜品分类下是否该 菜品   根据 categoryID查询 dish表
        if (countDish > 0){
            //有关联的菜品, 抛出一个异常..
            throw  new CustomException("当前分类下关联了菜品,不能删除");
        }

        //套餐
        LambdaQueryWrapper<Setmeal>  setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int countSetmeal = setmealService.count(setmealLambdaQueryWrapper);
        if (countSetmeal > 0){
            //有关联的套餐,抛出一个异常
            throw  new CustomException("当前分类下关联了套餐,不能删除");
        }
        //进行正常删除
        super.removeById(id);  //调用父类方法.
    }
}
